package seehope.reflect;

import java.lang.reflect.Method;

public class ReflectionDemo04 {
    public static void main(String[] args) {
        Class class1 = Person.class;
        Person person = new Person();
        try{
            System.out.println("---------获取所有公有方法（含Object类）---------");
            Method[] methods = class1.getMethods();
            for(Method method : methods){
                System.out.println(method.getName());
            }
            System.out.println("---------获取所有方法（不含Object类）---------");
            Method[] methods1 = class1.getDeclaredMethods();
            for(Method method : methods1){
                System.out.println(method.getName());
            }

            //获取单个方法使用
            System.out.println("---------获取单个方法并且使用（不带参数）---------");
            Method m1 = class1.getMethod("eat");
            m1.invoke(person);

            System.out.println("---------获取单个方法并且使用（带参数）---------");
            Method m2 = class1.getMethod("eat", int.class);
            m2.invoke(person, 5);

            System.out.println("---------获取单个私有方法并且使用---------");
            Method m3 = class1.getDeclaredMethod("sleep");
//            m3.invoke(person);
            m3.setAccessible(true);
            m3.invoke(person);

            System.out.println("---------判断方法的返回值类型---------");
            Method m4 = class1.getMethod("getAge");
            System.out.println(m4.getReturnType());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
